/**   
 * @Title: ShiyixuanwuMainThread.java 
 * @Package: com.esun.rabbit2.viewer.mainact.lotteryhallact 
 * @Description: 十一选五主界面的刷新线程
 * @Author: Miko baoz@500wan.com   
 * @Date: 2011-7-5 上午11:23:28  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.lotteryhallact;

import java.util.Hashtable;

import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.esun.config.Language;
import com.esun.rabbit2.communicaters.shiyixuanwu.TermInfoCommunicater;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.shiyixuanwu.TempData;
import com.esun.ui.R;

/**
 * @ClassName: ShiyixuanwuMainThread
 * @Description: 十一选五的主界面刷新线程
 * @Author: Miko baoz@500wan.com
 * @Date: 2011-7-5 上午11:23:28
 * 
 */
public class ShiyixuanwuMainThread {

	// 期号及剩余时间
	private TextView tvTerm;
	private TextView tvTime;
	// 进度条
	private ProgressBar pb;
	// 期号及剩余时间Layout
	private RelativeLayout rlShiyixuanwuLeftTime;

	private TermInfoCommunicater communicater = new TermInfoCommunicater();

	private Handler handler;

	private static final int GET_TERM_SUCCESS = 10001;
	private static final int GET_TERM_FAILED = 10002;

	private Hashtable<String, String> htTermInfo = new Hashtable<String, String>();

	private MyTimeLeftTimeCount ticker;

	private boolean isRunning = false;
	private boolean isCanbuy = false;
	// 是否是第一次加载
	private boolean isFirstLoading = true;

	// 颜色值
	private int color;

	public ShiyixuanwuMainThread(RelativeLayout rlShiyixuanwuLeftTime, ProgressBar pb, int color) {

		this.rlShiyixuanwuLeftTime = rlShiyixuanwuLeftTime;

		this.tvTerm = (TextView) rlShiyixuanwuLeftTime.findViewById(R.id.lotteryhall_act_tv_shiyixuanwuTerm);
		this.tvTime = (TextView) rlShiyixuanwuLeftTime.findViewById(R.id.lotteryhall_act_tv_shiyixuanwuTime);
		this.pb = pb;

		this.color = color;

		initHandler();
	}

	private void initHandler() {
		handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);

				progressGone();

				switch (msg.what) {
				case GET_TERM_SUCCESS:
					isCanbuy = true;
					isRunning = false;

					// 获取成功显示内容
					String strShow = Language.POPLOTTERYTIP_JULI + htTermInfo.get(TempData.EXPECT) + Language.POPLOTTERYTIP_QIJIEZHI;
					tvTerm.setText(strShow);

					// 期号及截止时间显示
					leftTimeVisible();

					// 计时
					resetTicker();

					break;

				case GET_TERM_FAILED:
					isCanbuy = false;
					isRunning = false;

					// 期号及截止时间不显示
					leftTimeGone();
					break;

				default:
					break;
				}
			}
		};
	}

	public void refresh() {

		if (!isRunning) {

			progressVisible();
			isCanbuy = false;
			// 第一次加载load页面
			if (isFirstLoading) {
				leftTimeGone();
				isFirstLoading = false;
			}

			if (ticker != null) {
				ticker.cancel();
			}

			new Thread() {

				@Override
				public void run() {
					super.run();
					isRunning = true;
					try {
						if (!doGetTermInfo()) {
							Thread.sleep(3000);
						}
					} catch (InterruptedException e) {
					}
				}
			}.start();
		}
	}

	private boolean doGetTermInfo() {

		if (communicater.getTermInfo()) {

			htTermInfo = communicater.getAvilableTerm();

			Message msg = handler.obtainMessage(GET_TERM_SUCCESS);
			handler.sendMessage(msg);

			return true;
		} else {

			Message msg = handler.obtainMessage(GET_TERM_FAILED);
			handler.sendMessage(msg);
			return false;
		}
	}

	private class MyTimeLeftTimeCount extends CountDownTimer {

		String strMin;
		String strSec;
		String strHour;
		long time;

		public MyTimeLeftTimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onTick(long millisUntilFinished) {

			time = millisUntilFinished / 1000;

			time = time % 86400;

			// 剩余小时
			strHour = String.valueOf(time / 3600);
			if (strHour.length() < 2) {
				strHour = TempData.NUM0 + strHour;
			}

			time = time % 3600;

			// 剩余分钟数
			strMin = String.valueOf(time / 60);
			if (strMin.length() < 2) {
				strMin = TempData.NUM0 + strMin;
			}

			// 剩余秒数
			strSec = String.valueOf(time % 60);
			if (strSec.length() < 2) {
				strSec = TempData.NUM0 + strSec;
			}

			// 2012-02-22 added by kuroky
			String strItem = strHour + Language.POPLOTTERYTIP_HOUR + strMin + Language.POPLOTTERYTIP_MINUTE + strSec + Language.POPLOTTERYTIP_SECOND;
			SpannableString sp = new SpannableString(strItem);

			sp.setSpan(new ForegroundColorSpan(color), 0, strItem.indexOf(Language.POPLOTTERYTIP_XIAO), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
			sp.setSpan(new ForegroundColorSpan(color), strItem.indexOf(Language.POPLOTTERYTIP_SHI) + 1, strItem.indexOf(Language.POPLOTTERYTIP_MINUTE), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
			sp.setSpan(new ForegroundColorSpan(color), strItem.indexOf(Language.POPLOTTERYTIP_MINUTE) + 1, strItem.indexOf(Language.POPLOTTERYTIP_SECOND), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

			tvTime.setText(sp);
		}

		@Override
		public void onFinish() {
			refresh();
		}
	}

	private void resetTicker() {
		if (ticker != null) {
			ticker.cancel();
		}

		ticker = new MyTimeLeftTimeCount(Long.parseLong(htTermInfo.get(TempData.LEFT_TIME)) * 1000, 1000);
		ticker.start();
	}

	public boolean isCanbuy() {
		return this.isCanbuy;
	}

	public void leftTimeVisible() {
		rlShiyixuanwuLeftTime.setVisibility(View.VISIBLE);
	}

	public void leftTimeGone() {
		rlShiyixuanwuLeftTime.setVisibility(View.GONE);
	}

	public void progressVisible() {
		pb.setVisibility(View.VISIBLE);
	}

	public void progressGone() {
		pb.setVisibility(View.GONE);
	}
	
}
